<?php
/**
 * @title 71. 简化路径 - 栈
 * @author start2004
 */

class Solution {

    /**
     * @param String $path
     * @return String
     */
    function simplifyPath($path) {
        /**
         * @since 2022-01-06 切割
         */
        $dataArray = explode("/", $path);

        /**
         * @since 2022-01-06 目录栈
         */
        $stackArray = [];

        /**
         * @since 2022-01-06 简化目录
         */
        foreach ($dataArray as $folder){
            switch ($folder){
                case "":
                case ".":
                    break;
                case "..":
                    array_pop($stackArray);
                    break;
                default:
                    $stackArray[] = $folder;
            }
        }

        /**
         * @return
         */
        return "/". implode("/", $stackArray);
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.30_13.php
 */
$datas = [
    ["/home/"], // /home
    ["/../"], // /
    ["/home//foo/"], // /home/foo
    ["/a/./b/../../c/"], // /c
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->simplifyPath(...$data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
